package com.imooc.controller;

import com.imooc.converter.OrderForm2OrderDTOConverter;
import com.imooc.dto.OrderDto;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.form.OrderForm;
import com.imooc.service.BuyerService;
import com.imooc.service.OrderService;
import com.imooc.utils.ResultVoUtils;
import com.imooc.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: <br>
 * (买家订单处理)
 *
 * @Author: Mr. xyb
 * @Date: 2018/5/18 17:12
 * @since: 1.8.0
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * (创建订单)
     *
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {// 传入的参数不符合规范则抛异常
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        // 对象转换
        OrderDto orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        // 如果购物车中的商品信息为空
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        /*创建订单*/
        OrderDto createResult = orderService.create(orderDTO);

        Map<String, String> map = new HashMap<>();
        /*返回订单id*/
        map.put("orderId", createResult.getOrderId());

        return ResultVoUtils.success(map);
    }

    /**
     * (分页查询订单列表)
     *
     * @param openid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVo<List<OrderDto>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // openid为空，抛异常
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        /*创建分页对象*/
        PageRequest request = new PageRequest(page, size);
        /*查询订单列表*/
        Page<OrderDto> orderDTOPage = orderService.findList(openid, request);
        return ResultVoUtils.success(orderDTOPage.getContent());
    }


    /**
     * (查询订单详情)
     *
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {
        // openid或者orderId为空，抛异常
        if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)) {
            log.error("【查询订单详情】参数为空 openid={},orderId", openid, orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        // 查询详情
        OrderDto orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVoUtils.success(orderDTO);
    }

    /**
     * (取消订单)
     *
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        // openid或者orderId为空，抛异常
        if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)) {
            log.error("【取消订单】参数为空 openid={},orderId", openid, orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        /*取消订单*/
        buyerService.cancelOrder(openid, orderId);
        return ResultVoUtils.success();
    }
}
